//****************************************************************************
//File: MasterDriver.java
//This java source file helps you run the matrix multiplication using the 
//JMS protocol by varying the size of the matrix and the number of slaves
//that need to be used in order to do the computation.
//***************************************************************************

/**
 * Class MasterDriver creates a Master object to perform 
 * matrix multiplication over varying sizes of matrix and
 * number of slaves used to perform the multiplication.
 *
 * Usage: java MasterDriver <I>size</I> <I>noofslaves</I> <I>host</I> <I>port</I>   
 * <BR><I>size</I> = Size of input square matrix.
 * <BR><I>noofslaves</I> = Number of slaves to be used to perform
 * 						   multiplication.
 * <BR><I>host</I> = Host name of the broker service
 * <BR><I>port</I> = Port number of the broker service
 * 
 * @author  Vivek Narang (vsn6751@rit.edu)
 * @version 05-May-2012
 */
public class MasterDriver {
// Main Program
	/** 
	 * Main Program
	 */
	public static void main(String[] args) {
		try {
			// Verify the command line arguments
			if(args.length != 4) usage();
			int size = parseInt(args[0], "size");
			int n = parseInt(args[1], "noofslaves");
			String host = args[2];
			int port = parseInt(args[3], "port");
			
			// Create a master object and perform multiplication
			Master master = new Master();
			master.performMultiplication(size, n, host, port);
		}
		catch(Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	/**
	* Print a usage message and exit.
	 */
	private static void usage() {
		System.err.println("MasterDriver Usage: java MasterDriver <size> <noofslaves> <host> <port>");
		System.err.println("<size> is the size of the input matrix.");
		System.err.println("<noofslaves> is the number of slaves used.");
		System.err.println("<host> is the Host name of the broker service.");
		System.err.println("<port> is the Port number of the broker service.");
		System.exit(1);
	}
	
	/**
	 * Parse an integer command line argument.
	 *
	 * @param  arg  Command line argument.
	 * @param  name  Argument name.
	 *
	 * @return  Integer value of <TT>arg</TT>.
	 *
	 * @exception  NumberFormatException
	 *     Thrown if <TT>arg</TT> cannot be parsed 
	 *     as an integer.
	 */
	private static int parseInt(String arg, String name) {
		try {
			return Integer.parseInt (arg);
		}
		catch (NumberFormatException exc) {
			System.err.println("MasterDriver: Invalid " + name + 
				": \"" + arg + "\"");
			usage();
			return 0;
		}
	}
}